﻿
namespace ModuleManagerService.Web
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Data;
    using System.Linq;
    using System.Web.DomainServices;
    using System.Web.DomainServices.Providers;
    using System.Web.Ria;
    using System.Web.Ria.Services;


    // Implements application logic using the ModulesDBEntities context.
    // TODO: Add your application logic to these methods or in additional methods.
    // TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access
    // Also consider adding roles to restrict access as appropriate.
    // [RequiresAuthentication]
    [EnableClientAccess()]
    public class ModuleManagerService : LinqToEntitiesDomainService<ModulesDBEntities>
    {
        protected override void OnError(DomainServiceErrorInfo errorInfo)
        {
            base.OnError(errorInfo);
        }

        // TODO: Consider
        // 1. Adding parameters to this method and constraining returned results, and/or
        // 2. Adding query methods taking different parameters.
        public IQueryable<InitializationMode> GetInitializationModes()
        {
            return this.ObjectContext.InitializationModes;
        }

        public void InsertInitializationMode(InitializationMode initializationMode)
        {
            this.ObjectContext.AddToInitializationModes(initializationMode);
        }

        public void UpdateInitializationMode(InitializationMode currentInitializationMode)
        {
            if ((currentInitializationMode.EntityState == EntityState.Detached))
            {
                this.ObjectContext.AttachAsModified(currentInitializationMode, this.ChangeSet.GetOriginal(currentInitializationMode));
            }
        }

        public void DeleteInitializationMode(InitializationMode initializationMode)
        {
            if ((initializationMode.EntityState == EntityState.Detached))
            {
                this.ObjectContext.Attach(initializationMode);
            }
            this.ObjectContext.DeleteObject(initializationMode);
        }

        // TODO: Consider
        // 1. Adding parameters to this method and constraining returned results, and/or
        // 2. Adding query methods taking different parameters.
        public IQueryable<Module> GetModules()
        {
            return this.ObjectContext.Modules.Include("InitializationMode");
        }

        public void InsertModule(Module module)
        {
            this.ObjectContext.AddToModules(module);
        }

        public void UpdateModule(Module currentModule)
        {
            if ((currentModule.EntityState == EntityState.Detached))
            {
                this.ObjectContext.AttachAsModified(currentModule, this.ChangeSet.GetOriginal(currentModule));
            }
        }

        public void DeleteModule(Module module)
        {
            if ((module.EntityState == EntityState.Detached))
            {
                this.ObjectContext.Attach(module);
            }
            this.ObjectContext.DeleteObject(module);
        }
    }
}


